<?php

class Mithos_News {
    const TYPE_NOTICE = 1;
    const TYPE_UPDATE = 2;
    const TYPE_EVENT = 3;
    const TYPE_PROMOTION = 4;
    
    private $_id;
    private $_title;
    private $_author;
    private $_type;
    private $_body;
    private $_created;
    private $_comments;
    private $_draft;

    public function __construct($id = null) {
        if ($id !== null) {
            $this->read($id);
        }
    }

    public function id($id = null) {
        if ($id === null) {
            return $this->_id;
        }
        $this->_id = $id;
        return $this;
    }

    public function title($title = null) {
        if ($title === null) {
            return $this->_title;
        }
        $this->_title = $title;
        return $this;
    }

    public function author($author = null) {
        if ($author === null) {
            if (!$this->_author instanceof Mithos_Character) {
                return new Mithos_Character($author);
            }
            return $this->_author;
        }
        $this->_author = $author;
        return $this;
    }

    public function type($type = null) {
        if ($type === null) {
            return $this->_type;
        }
        $this->_type = $type;
        return $this;
    }

    public function body($body = null) {
        if ($body === null) {
            return $this->_body;
        }
        $this->_body = $body;
        return $this;
    }

    public function created($created = null) {
        if ($created === null) {
            return $this->_created;
        }
        $this->_created = $created;
        return $this;
    }

    public function draft($draft = null) {
        if ($draft === null) {
            return empty($this->_draft) ? false : (bool)$this->_draft;
        }
        $this->_draft = $draft;
        return $this;
    }

    public function comments() {
        $results = Mithos_Mssql::getInstance()->fetchAll('SELECT 
			id FROM Mithos.dbo.news_comments 
			WHERE id = :id[integer]
		', array('id' => $this->id()));
        if (!empty($results)) {
            foreach ($results as $result) {
                $this->_comments[] = new Mithos_News_Comment($result['id']);
            }
        }
        return $this->_comments;
    }

    public function exists() {
        return $this->id() !== null;
    }

    public function all($top = null) {
        $top = $top === null ? '' : ' TOP ' . $top;
        $results = Mithos_Mssql::getInstance()->fetchAll('SELECT' . $top . ' *
            FROM Mithos.dbo.news 
            ORDER BY id DESC
        ');
        $data = array();
        foreach ($results as $result) {
            $news = new self();
            foreach ($result as $key => $value) {
                if (method_exists($this, $key)) {
                    $news->{$key}($value);
                }
            }
            $data[] = $news;
        }
        return $data;
    }

    public function read($id = null) {
        $result = Mithos_Mssql::getInstance()->fetch('SELECT *, 
            (SELECT COUNT(1) FROM Mithos.dbo.news_comments WHERE Mithos.dbo.news_comments.news_id = Mithos.dbo.news.id) AS count_comments 
            FROM Mithos.dbo.news 
            WHERE id = :id[integer]
        ', array('id' => $id));
        if (!empty($result)) {
            foreach ($result as $key => $value) {
                if (method_exists($this, $key)) {
                    $this->{$key}($value);
                }
            }
        }
    }

    public function insert() {
        $values = array(
            'title' => $this->title(),
            'body' => $this->body(),
            'type' => $this->type(),
            'author' => $this->author()->name(),
            'draft' => $this->draft()
        );
        Mithos_Mssql::getInstance()->query('INSERT INTO Mithos.dbo.news (title, body, author, type, display_comments, draft, created) VALUES (:title[string], :body[string], :author[string], :type[integer], 1, :draft[boolean], GETDATE())', $values);
    }

    public function update($id = null) {
        $values = array(
            'id' => (int) ($id === null) ? $this->id() : $id,
            'title' => $this->title(),
            'body' => $this->body(),
            'type' => (int) $this->type(),
            'author' => $this->author()->name(),
            'draft' => $this->draft()
        );
        Mithos_Mssql::getInstance()->query('UPDATE Mithos.dbo.news SET title = :title[string], body = :body[string], author = :author[string], type = :type[integer], display_comments = 1, draft = :draft[boolean] WHERE id = :id[integer]', $values);
    }

    public function delete($id = null) {
        Mithos_Mssql::getInstance()->query('DELETE FROM 
            Mithos.dbo.news 
            WHERE id = :id[integer]
        ', array('id' => (int) ($id === null) ? $this->id() : $id
        ));
        return true;
    }
}

?>